import Database from "@tauri-apps/plugin-sql";
import { create } from '@tauri-apps/plugin-fs';
import { homePath, createDirs, uuid, getG01DataBase, saveText } from '../../utils';
import { FlowProject } from "../types";
import { BlockEnum } from "@/components/workflow/types";

const FLOW_PROJECTS_TABLE_NAME = 'flow_projects';
const FLOW_PROJECT_TAGS_TABLE_NAME = 'flow_project_tags';
const FLOW_PROJECT_ENVS_TABLE_NAME = 'flow_project_envs';

export const flowGetProjects = async (page: number, pageSize: number, tag?: Array<string>): Promise<{ total: number, data: Array<FlowProject> }> => {
    const db = await getG01DataBase();

    let offset = (page - 1) * pageSize;

    let conditions = ` `;
    let args = [];
    if (tag && tag.length > 0) {
        conditions = ` `;
    }

    let count = (await db.select(`select count(1) cnt from ${FLOW_PROJECTS_TABLE_NAME} ${conditions}`, args))[0].cnt;
    //
    let items: Array<any> = await db.select(`select * from  ${FLOW_PROJECTS_TABLE_NAME} ${conditions} order by id desc limit ${offset},${pageSize} `, args);
    if (items) {
        return {
            total: count,
            data: items,
        };
    }

    return {
        total: 0,
        data: []
    }
}

export const flowGetProjectsByEmptyTag = async (page: number, pageSize: number): Promise<{ total: number, data: Array<FlowProject> }> => {
    const db = await getG01DataBase();

    let offset = (page - 1) * pageSize;

    let conditions = ` `;
    let args = [];

    let count = (await db.select(`select count(1) cnt from ${FLOW_PROJECTS_TABLE_NAME} ${conditions}`, args))[0].cnt;
    //
    let items: Array<any> = await db.select(`select * from  ${FLOW_PROJECTS_TABLE_NAME} ${conditions} order by id desc limit ${offset},${pageSize} `, args);
    if (items) {
        return {
            total: count,
            data: items,
        };
    }

    return {
        total: 0,
        data: []
    }
}

export const flowGetProjectTags = async (): Promise<Array<string>> => {
    const db = await getG01DataBase();
    //
    let items: Array<string> = await db.select(`select distinct tag from  ${FLOW_PROJECT_TAGS_TABLE_NAME}`, []);
    if (items) {
        return items;
    }

    return []
}


export const flowGetProjectEnvs = async (projectIdentifier: string): Promise<Array<string>> => {
    const db = await getG01DataBase();
    //
    let items: Array<string> = await db.select(`select distinct tag from  ${FLOW_PROJECT_TAGS_TABLE_NAME}`, []);
    if (items) {
        return items;
    }

    return []
}

export const flowSaveProject = async (obj: FlowProject): Promise<number> => {

    // _g01_meta_class_attributes
    const db = await getG01DataBase();

    let items: Array<any> = await db.select(`select * from  ${FLOW_PROJECTS_TABLE_NAME} where identifier = $1 `, [obj.identifier]);
    if (items && items.length > 0) {
        //更新
        return await flowUpdateProject(obj);
    } else {
        //
        await db.execute(`insert into ${FLOW_PROJECTS_TABLE_NAME}(identifier,name,icon,path,project_type,options,description)Values($1,$2,$3,$4,$5,$6,$7)`,
            [obj.identifier, obj.name, obj.icon, obj.path, obj.projectType, obj.options, obj.description]);

        let res: Array<any> = await db.select('SELECT last_insert_rowid() nid');
        console.log(res);
        if (res && res.length > 0) {
            return res[0].nid;
        }
        return 0;
    }
}

export const flowUpdateProject = async (obj: FlowProject): Promise<number> => {

    // _g01_meta_class_attributes
    const db = await getG01DataBase();
    //
    await db.execute(`update ${FLOW_PROJECTS_TABLE_NAME} set name = $2, icon = $3, path = $4, options = $5, description = $6 where identifier = $1`,
        [obj.identifier, obj.name, obj.icon, obj.path, obj.options, obj.description]);
    return 0;
}
 